﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace CPPEI.Coolzon.Designer.UI
{
    public abstract class PropertyValueEditor : UserControl
    {
        public object PropertyValue
        {
            get
            {
                return GetValue(PropertyValueProperty);
            }
            set
            {
                SetValue(PropertyValueProperty, value);
            }
        }

        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(object), typeof(PropertyValueEditor),
            new PropertyMetadata(null, OnPropertyValuePropertyChangedCallback));

        public static void OnPropertyValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PropertyValueEditor editor = d as PropertyValueEditor;
            editor.OnPropertyValueChanged(e.NewValue);
        }

        /// <summary>
        /// 绑定到对象的属性上
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="xProperty"></param>
        /// <param name="propertyInfo"></param>
        public void BindPropertyValue(object obj, XPropertyAttribute xProperty, PropertyInfo propertyInfo)
        {
            Binding binding = new Binding(propertyInfo.Name);
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueEditor.PropertyValueProperty, binding);
            this.DataContext = obj;
        }

        /// <summary>
        /// 值变化
        /// </summary>
        /// <param name="newValue"></param>
        protected abstract void OnPropertyValueChanged(object newValue);

        protected void ChangePropertyValue(object newValue)
        {
            PropertyValue = newValue;
        }
    }
}
